package NetWork.Exercise006;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * ClassName:Server
 * Package:NetWork.Exercise001
 * Description:
 *
 * @Author:ZhangXin
 * @Create:2023/3/7-22:41
 * @Version:v1.0
 */
public class Server {
    public static void main(String[] args) throws IOException {

        //创建线程池对象
        ThreadPoolExecutor pool = new ThreadPoolExecutor(
                3,//核心线程池数量
                16,//线程池大小
                60,//空闲时间
                TimeUnit.SECONDS,//空闲时间单位
                new ArrayBlockingQueue<>(2),//队列
                Executors.defaultThreadFactory(),//线程工厂,让线程如何创建线程对象
                new ThreadPoolExecutor.AbortPolicy()//阻塞队列
        );

        //创建对象并连接端口
        ServerSocket serverSocket = new ServerSocket(1002);
        //等待客户端来连接

        while (true) {
            //等待客户端来连接
            Socket socket = serverSocket.accept();
            //开启一条线程
            //一个用户就对应服务器的一条线程
//            new Thread(new MyRunnable(socket)).start();
            pool.submit(new MyRunnable(socket));

        }


    }
}
